/* eslint-disable no-undef */
import type { RouteRecordRaw } from 'vue-router'
import routes from './routes'

const basePath = '/mobile'

// 根据path动态导入对应的组件
// 这一段类型太难写，勉强用吧
const getMobileRoutes = (routes: MobileRoute[], parentPath = ''): RouteRecordRaw[] => {
  // @ts-ignore
  return routes.map(route => {
    let fullPath = ''
    if (route.path === '/') {
      fullPath = ''
    } else if (route.path.startsWith('/')) {
      fullPath = parentPath + route.path
    } else {
      fullPath = parentPath + '/' + route.path
    }

    const renderPath = route.componentPath || fullPath
    if (route.children) {
      route.children = getMobileRoutes(route.children, fullPath) as MobileRoute[]
    }

    // vite的import.meta.glob方法必须是一个字符串，不能动态拼接，所以这里需要写死
    const modules = import.meta.glob('../../views/mobile/**/**/*.vue')

    let component
    if (route.path !== '/') {
      component = modules[`../../views${basePath}${renderPath}/index.vue`]
    }

    return {
      ...route,
      component,
    }
  })
}

export default getMobileRoutes(routes)
